
#include "netinet/in.h"
#include "arpa/inet.h"
#include "sys/types.h"
#include "sys/socket.h"
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#include "time.h"

#include <unistd.h>
#include <fcntl.h>


#define MAX_LISTEN_QUE		5
#define MAX_WRITE_BUF_SIZE	200

int main(int argc, char *argv[])
{
	int listen_fd, sock_fd;
	int len;
	int err;
	time_t timep;
	char write_buf[MAX_WRITE_BUF_SIZE];
	struct sockaddr_in svr;
	struct sockaddr_in client;


	listen_fd = socket(AF_INET, SOCK_STREAM, 0);
	if(listen_fd < 0){
		perror("socket failed");
		return -1;
	}

	svr.sin_family			= AF_INET;
	svr.sin_addr.s_addr		= inet_addr("192.168.56.101");
	//svr.sin_addr.s_addr		= htonl(INADDR_ANY);
	svr.sin_port			= htons(8888);
	len						= sizeof(svr);

	err = bind(listen_fd, (struct sockaddr *)&svr, len);
	if(err < 0){
		perror("bind failed");
		return -1;
	}

	listen(listen_fd, MAX_LISTEN_QUE);

	while(1){
		sock_fd = accept(listen_fd, (struct sockaddr *)&client, &len);
		if(sock_fd < 0){
			perror("accept failed");
			return -1;
		}

		timep = time(NULL);
		snprintf(write_buf, sizeof(write_buf),"%s", ctime(&timep));
		write(sock_fd, write_buf, strlen(write_buf));
		
		printf("bytes: %d\n", len);
		printf("sockfd: %d\n", sock_fd);

		close(sock_fd);
	}

	return 0;
}
